// pages/list/list.js
var app = getApp();
var liunxUrl=app.globalData.liunxUrl
var localUrl=app.globalData.localUrl


Page({

  /**
   * 页面的初始数据
   */
  data: {
    date:"",
    user:{},
    list:[],
    del:false
  },

  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value
    })
    this.data.date = e.detail.value;
    this.getMonth(e.detail.value+"-01");
  },

  getMonth(month){
    wx.request({
      url: liunxUrl+'/rec/queryMonthBill?userId='+this.data.user.id+'&month='+month,
      method:'get',
      success:(res)=>{
        console.log(res)
        if(res.data.code==200){
            let newList = [];
            newList = res.data.data;

            newList.forEach(item=>{
              if(item.type===0){
                item.typeName = "收入";
              }else if(item.type===1){
                item.typeName = "支出";
              }else if(item.type===2){
                item.typeName = "投资";
              }else if(item.type===3){
                item.typeName = "还款";
              }
              else if(item.type===4){
                item.typeName = "购物";
              }
              else if(item.type===5){
                item.typeName = "房租";
              }
            })
            this.setData({
              list:newList
            })
        
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.user = app.globalData.user;
    this.setData({
      user:app.globalData.user
    });
    let month = this.dateToString(new Date);
      this.setData({
        date: this.dateToString1(new Date)
      })
      this.getMonth(month);
  },

  handleLongPress(){
    this.setData({
      del:true
    })
  },
  exit(){
    this.setData({
      del:false
    })
  },
  delete(e){

    wx.showModal({
      title: '删除提醒',
      content: '是否确定删除',
      success: res=> {
          if (res.confirm) {
            wx.showLoading({
              title: '删除中',
              mask: true,
            })

            wx.request({
              url: liunxUrl+'/rec/delete?id='+e.currentTarget.dataset.id,
              method:'get',
              success:(res)=>{
                if(res.data.code===200){

                  wx.hideLoading({
                    complete: (res) => {},
                  })
                  this.data.list.splice(e.currentTarget.dataset.index,1);
                  this.setData({
                    list:this.data.list
                  })
                }else{
                  wx.showToast({
                      title: '删除失败',
                      image:"/pages/image/jg.png",
                     duration:2000
                 })
                }
              }
            })
          } 
      }
  })

   
  },
  dateToString(date){ 
    var year = date.getFullYear(); 
    var month =(date.getMonth() + 1).toString(); 
    var day = (date.getDate()).toString();  
    if (month.length == 1) { 
        month = "0" + month; 
    } 
    if (day.length == 1) { 
        day = "0" + day; 
    }
    var dateTime = year + "-" + month + "-" + day;
    return dateTime; 
  },
  dateToString1(date){ 
    var year = date.getFullYear(); 
    var month =(date.getMonth() + 1).toString(); 
    var day = (date.getDate()).toString();  
    if (month.length == 1) { 
        month = "0" + month; 
    } 
    if (day.length == 1) { 
        day = "0" + day; 
    }
    var dateTime = year + "-" + month;
    return dateTime; 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})